package com.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.action.AdminAction;
import com.bean.AdminBean;
import com.exception.AdminException;
import com.exception.DBException;
import com.util.HandleInfo;
import com.util.Regex;
import com.util.StateInfo;

public class AdminModifyServlet extends HttpServlet
{

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		String option = request.getParameter("option");
		if(null == option)
		{
			HandleInfo.handle(request, response, StateInfo.INNER_CONFIRM_FAIL);
			return;
		}
		if ("edit".equals(option))
		{
			try
			{
				new AdminAction().getAdmin(request, response,
						(AdminBean) request.getSession().getAttribute("admin"));
				return;
			}
			catch (DBException e)
			{
				HandleInfo.handle(request, response, e.getMessage());
			}
			catch (AdminException e)
			{
				HandleInfo.handle(request, response, e.getMessage());
			}
		}
		if("submit".equals(option))
		{
			String adminid = request.getParameter("adminid");
			String firstname = request.getParameter("firstname");
			String lastname = request.getParameter("lastname");
			String password = request.getParameter("password");
			String email = request.getParameter("email");
			String remark = request.getParameter("remark");
			// validate the information
			
			if (null == adminid || null == firstname || null == lastname
					|| null == password || null == email || null == remark)
			{
				HandleInfo.handle(request, response,
						StateInfo.INNER_CONFIRM_FAIL);
				return;
			}
			if ("".equals(adminid) || "".equals(firstname)
					|| "".equals(lastname) || "".equals(email)
					|| "".endsWith(password))
			{
				HandleInfo.handle(request, response,
						StateInfo.INNER_CONFIRM_FAIL);
				return;
			}
			if (adminid.length() != 8 || firstname.length() > 30
					|| lastname.length() > 30 || password.length() > 10
					|| email.length() > 50)
			{
				HandleInfo.handle(request, response,
						StateInfo.INNER_CONFIRM_FAIL);
				return;
			}
			if (!Regex.checkUserID(adminid) || !Regex.checkName(firstname)
					|| !Regex.checkName(lastname) || !Regex.checkEmail(email))
			{
				HandleInfo.handle(request, response,
						StateInfo.INNER_CONFIRM_FAIL);
				return;
			}
			AdminBean adminBean = new AdminBean();
			adminBean.setAdminID(adminid);
			adminBean.setFirstname(firstname);
			adminBean.setLastname(lastname);
			adminBean.setPassword(password);
			adminBean.setEmail(email);
			adminBean.setRemark(remark);
			try
			{
				new AdminAction().updateAdmin(request, response, adminBean);
			}
			catch (DBException e)
			{
				HandleInfo.handle(request, response, e.getMessage());
			}
			catch (AdminException e)
			{
				HandleInfo.handle(request, response, e.getMessage());
			}
		}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		doGet(request, response);
	}

}
